package com.tupu.service.impl.permission;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tupu.entity.permission.RoleInfo;
import com.tupu.entity.permission.RolePermission;
import com.tupu.mapper.permission.PermissionListMapper;
import com.tupu.mapper.permission.RoleMapper;
import com.tupu.mapper.permission.RolePermissionMapper;
import com.tupu.service.permission.RoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired(required = false)
    private RoleMapper roleMapper;

    @Autowired(required = false)
    private RolePermissionMapper rolePermissionMapper;

    @Autowired(required = false)
    private PermissionListMapper permissionListMapper;

    @Override
    public void delRole(List<Integer> role_id) {
        for (Integer id : role_id) {
            roleMapper.deleteById(id);
            rolePermissionMapper.deleteByRoleId(id);
        }
    }

    @Override
    public List<RoleInfo> findAll() {
        List<RoleInfo> roleInfo = roleMapper.selectList(null);
        return roleInfo;
    }

    @Override
    public Page<RoleInfo> PageInfo(Integer page, Integer size, String name) {
        QueryWrapper<RoleInfo> queryWrapper = new QueryWrapper<>();
        Page<RoleInfo> pageInfo = new Page<>(page, size);
        if (StringUtils.isNotEmpty(name)) {
            queryWrapper.like("role_name", name);
        }
        queryWrapper.orderByAsc("role_id");
        roleMapper.selectPage(pageInfo, queryWrapper);
        return pageInfo;
    }

    @Override
    public RoleInfo findById(Integer role_id) {
        RoleInfo role = roleMapper.selectById(role_id);
        String one = rolePermissionMapper.findOne(role_id);
        String[] ids = one.split(",");
        List<Integer> num = Stream.of(ids).map(Integer::parseInt).collect(Collectors.toList());
        List<String> list = permissionListMapper.findDescribe(one);
        role.setPermissionIds(num);
        role.setPermissionList(list);
        return role;
    }

    @Override
    public Integer updateRole(RoleInfo role) {
        Integer roleId = role.getRoleId();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String format = sdf.format(date);
        RoleInfo roleInfo = roleMapper.selectById(roleId);
        String per = role.getPermissionIds().stream().map(String::valueOf).collect(Collectors.joining(","));
        if (roleInfo.getRoleName().equals(role.getRoleName())) {
            role.setUpdateTime(format);
            roleMapper.updateById(role);
            rolePermissionMapper.updateByRoleId(per, roleId);
            return 1;
        }
        Integer num = roleMapper.findCountByname(role.getRoleName());
        if (0 == num) {
            RoleInfo newRole = new RoleInfo();
            newRole.setRoleId(role.getRoleId());
            newRole.setStatus(role.getStatus());
            newRole.setRoleName(role.getRoleName());
            newRole.setPermissionIds(role.getPermissionIds());
            newRole.setUpdateTime(format);
            roleMapper.updateById(newRole);
            rolePermissionMapper.updateByRoleId(per, roleId);
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public Integer addRole(RoleInfo role) {
        List<Integer> permission_id = role.getPermissionIds();
        Integer num = roleMapper.findCountByname(role.getRoleName());
        if (0 != num) {
            return 0;
        }
        String per = permission_id.stream().map(String::valueOf).collect(Collectors.joining(","));
        //if (per.matches("\\d*")) {
        //    return "权限信息输入有误";
        //}
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String format = sdf.format(date);
        role.setUpdateTime(format);
        roleMapper.insert(role);
        RolePermission rolePermission = new RolePermission();
        rolePermission.setRoleId(role.getRoleId());
        rolePermission.setPermissionId(per);
        rolePermissionMapper.insert(rolePermission);
        //添加成功
        return 1;
    }

}